package com.ljx.recruitment.security.interceptor;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ljx.recruitment.common.exception.ApiException;
import com.ljx.recruitment.sale.domain.entity.AccessToken;
import com.ljx.recruitment.sale.service.AccessTokenService;
import com.ljx.recruitment.security.common.UserContext;
import com.ljx.recruitment.security.util.UserUtil;
import com.ljx.recruitment.user.domain.dto.UserDto;
import com.ljx.recruitment.user.domain.entity.User;
import com.ljx.recruitment.user.domain.vo.resp.TenantResp;
import com.ljx.recruitment.user.enums.CommonRoleEnum;
import com.ljx.recruitment.user.service.TenantService;
import com.ljx.recruitment.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-10-01  19:11
 */
@Component("accessTokenInterceptor")
public class AccessTokenInterceptor implements HandlerInterceptor {

    @Autowired
    private AccessTokenService accessTokenService;

    @Autowired
    private UserService userService;

    @Autowired
    private TenantService tenantService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        String token = request.getHeader("accessToken");

        if (StrUtil.isEmpty(token)){
            return true;
        }
        AccessToken accessToken = accessTokenService.getByToken(token);
        if (Objects.isNull(accessToken)){
            throw new ApiException("身份验证失败");
        }
        TenantResp platform = tenantService.getInfoByCode(CommonRoleEnum.PLATFORM.getKey());
        User user = userService.getByUsernameAndTenant("anonymous", platform.getId());
        UserContext.setUser(getUserDto(user));

        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserContext.removeUser();
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }

    private UserDto getUserDto(User user) {
        UserDto userDto = BeanUtil.copyProperties(user, UserDto.class);
        userDto.setPermissions(UserUtil.tryToGetUserPermission(user.getId()));
        userDto.setRoles(UserUtil.tryToGetUserRole(user.getId()));
        userDto.setMenus(UserUtil.tryToGetUserMenu(user.getId()));
        return userDto;
    }
}
